home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993…ch: Other People's Memory / ADC Developer CD (1993-03) (''Other People's Memory'')_iso / Dev.CD Mar 93.iso / Technical Documentation / Sample Code / DTS.Lib & Samples / Kibitz / IdleTasks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-22  |  4.8 KB  |  182 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        idletasks.c
  5. ** Written by:  Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved. */
  9.  
  10.  
  11.  
  12. /*****************************************************************************/
  13.  
  14.  
  15.  
  16. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  17. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  18. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  19.  
  20. #ifndef __UTILITIES__
  21. #include <Utilities.h>
  22. #endif
  23.  
  24.  
  25.  
  26. /*****************************************************************************/
  27.  
  28.  
  29.  
  30. #pragma segment Main
  31. void    DoIdleTasks(Boolean allowComputerMoves)
  32. {
  33.     WindowPtr        window, compMoveWindow;
  34.     unsigned long    compMoveTick;
  35.     FileRecHndl        game, compMoveGame;
  36.     FileRecPtr        frPtr;
  37.     short            twoPlayer, moveColor, myColor, update, syncClocks, i;
  38.     Boolean            compMovesWhite, compMovesBlack, sendSyncGame;
  39.  
  40.     static unsigned long    clockSyncTick;
  41.  
  42.     DynamicBalloonHelp();
  43.  
  44.     compMoveTick = -1;
  45.  
  46.     if (syncClocks = (clockSyncTick + 1800 < TickCount()))
  47.         clockSyncTick = TickCount();
  48.             /* Syncronize clocks every 30 seconds. */
  49.  
  50.  
  51.     for (window = FrontWindow();
  52.          window;
  53.          window = (WindowPtr)(((WindowPeek)window)->nextWindow)
  54.     ) {
  55.         if (IsAppWindow(window)) {
  56.  
  57.             DoSetCursor(nil);
  58.  
  59.             game = (FileRecHndl)GetWRefCon(window);
  60.             twoPlayer = (*game)->doc.twoPlayer;
  61.  
  62.             if (update = UpdateTime(game, true)) DrawTime(game);
  63.  
  64.             if (update == 2) {
  65.                 if (twoPlayer) SendGame(game, kIsMove, nil);
  66.                     /* Send it as a move, since we want the alert to
  67.                     ** show up for the opponent. */
  68.                 AlertIfGameOver(game);
  69.                 return;
  70.             }
  71.  
  72.             if ((*game)->doc.resync >= kResync) {
  73.                 sendSyncGame = true;
  74.                     /* We may need to sync up.  Assume we will. */
  75.  
  76.                 if ((*game)->doc.configColorChange) {
  77.                     if ((*game)->doc.myColor != (*game)->doc.configColor) {
  78.                         (*game)->doc.myColor = (*game)->doc.configColor;
  79.                         (*game)->doc.invertBoard ^= 1;
  80.                         SetPort(window);
  81.                         ImageDocument(game, true);
  82.                     }
  83.                     (*game)->doc.configColorChange = false;
  84.                     AdjustGameSlider(game);
  85.                     sendSyncGame = false;
  86.                 }
  87.  
  88.                 if ((*game)->doc.configTimeChange) {
  89.                     for (i = 0; i < 2; ++i)
  90.                         (*game)->doc.timeLeft[i] =
  91.                             (*game)->doc.displayTime[i] =
  92.                                 (*game)->doc.configTime[i];
  93.                     (*game)->doc.configTimeChange = false;
  94.                     UpdateTime(game, false);
  95.                     DrawTime(game);
  96.                     sendSyncGame = false;
  97.                 }
  98.  
  99.                 if (sendSyncGame) {
  100.                     if ((*game)->doc.gotUpdateTick + 120 < TickCount()) {
  101.                         /* Wait for 2 secs since last game update before syncing.
  102.                         ** This is so that we don't send a sync while the opponent
  103.                         ** is still clicking on the arrow.  Without this delay, the
  104.                         ** scrollbar of the opponent will jump around after he is
  105.                         ** done scrolling.  It would eventually end up correct,
  106.                         ** but it looks bad.  2 secs is enough time (generally) for
  107.                         ** the opponent to receive the sync from the last click, so
  108.                         ** the scroll won't jump around.  Also, 2 secs is probably
  109.                         ** more time than the user would take between clicks on
  110.                         ** the arrow. */
  111.  
  112.                         if ((*game)->doc.creator)        /* Only the creator can sync. */
  113.                             if (twoPlayer) SendGame(game, kHandResync, nil);
  114.                                 /* Make sure the boards are in sync. */
  115.  
  116.                         (*game)->doc.resync = kIsMove;
  117.                             /* Back to life as usual. */
  118.  
  119.                         DoSetCursor(nil);
  120.                             /* Re-calc the cursor. */
  121.                     }
  122.                 }
  123.             }
  124.  
  125.             frPtr          = *game;
  126.             compMovesWhite = frPtr->doc.compMovesWhite;
  127.             compMovesBlack = frPtr->doc.compMovesBlack;
  128.             myColor        = frPtr->doc.myColor;
  129.             moveColor      = WhosMove(game);
  130.  
  131.             if (frPtr->doc.arrangeBoard)
  132.                 compMovesWhite = compMovesBlack = 0;
  133.  
  134.             if (twoPlayer) {
  135.                 if (myColor == moveColor) {
  136.                     if (syncClocks)
  137.                         SendMssg(game, kTimeMssg);
  138.                             /* Sync up the clocks every 30 seconds.  Only do
  139.                             ** this if it is our move, since the player who
  140.                             ** owns the move also owns the clock. */
  141.                 }
  142.                 else
  143.                     compMovesWhite = compMovesBlack = false;
  144.             }
  145.             if (
  146.                 ((compMovesWhite) && (moveColor == WHITE)) ||
  147.                 ((compMovesBlack) && (moveColor == BLACK))
  148.             ) {
  149.                 if (compMoveTick > frPtr->doc.compMoveTick) {
  150.                     if (GameStatus(game) == kGameContinues) {
  151.                         compMoveTick   = frPtr->doc.compMoveTick;
  152.                         compMoveWindow = window;
  153.                         compMoveGame   = game;
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.     }
  159.  
  160.     if (allowComputerMoves) {
  161.         if (compMoveTick != -1) {
  162.             if ((*compMoveGame)->doc.resync == kIsMove) {
  163.                 (*compMoveGame)->doc.compMoveTick = TickCount();
  164.                 if (ComputerMove(compMoveGame)) {
  165.                     SetPort(compMoveWindow);
  166.                     ImageDocument(compMoveGame, true);
  167.                     AdjustGameSlider(compMoveGame);
  168.                     twoPlayer = (*compMoveGame)->doc.twoPlayer;
  169.                     DrawButtonTitle(compMoveGame, twoPlayer);
  170.                     UpdateGameStatus(compMoveGame);
  171.                     if (twoPlayer) SendGame(compMoveGame, kIsMove, nil);
  172.                     AlertIfGameOver(compMoveGame);
  173.                     DoSetCursor(nil);
  174.                 }
  175.             }
  176.         }
  177.     }
  178. }
  179.  
  180.  
  181.  
  182.